package com.example.test.springdemo.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ,;,,;
 * ,;;'(    社
 * __      ,;;' ' \   会
 * /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 * ,;' \    /-.,,(   ) \    码
 * ) /       ) / )|    农
 * ||        ||  \)
 * (_\       (_\
 *
 * @author ：吉金武
 * @date ：Created in 2020/11/5
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description:
 **/
@MapperScan(value = {"com.example.test.springdemo.mapper"}, sqlSessionFactoryRef = "tGouMallBuzzSqlSessionFactory")
@Configuration
public class TgouMallBuzzDaoConfig implements InitializingBean {

    @ConfigurationProperties(value = "datasource.druid")
    @Bean(value = "tGouMallBuzzDataSource")
    public DataSource druidDataSource2() {
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }

    @Bean(value = "tGouMallBuzzTransactionManager")
    public PlatformTransactionManager transactionManager2(@Qualifier("tGouMallBuzzDataSource") @Autowired DataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
        return dataSourceTransactionManager;
    }


    @Bean("tGouMallBuzzSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("tGouMallBuzzDataSource") @Autowired DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setMapperLocations(resolveResource());
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        factory.setConfiguration(configuration);
        return factory.getObject();
    }

    @Bean("tGouMallBuzzSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("tGouMallBuzzSqlSessionFactory") @Autowired SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        ((DruidDataSource) druidDataSource2()).init();
    }

    /**
     * 扫描mapper.xml
     * @return
     * @throws IOException
     */
    protected Resource[] resolveResource() throws IOException {
        PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
        String[] location={"classpath:/mapper/*.xml"};
        List<Resource> resources=new ArrayList<>();
        for (String locations:location){
            Resource[] lr=pathMatchingResourcePatternResolver.getResources(locations);
            resources.addAll(Arrays.asList(lr));
        }
        return resources.toArray(new Resource[resources.size()]);
    }
}
